/*
 * revaDAO.java
 *
 * Created on March 17, 2007, 2:14 PM
 */

package mystuff;

import java.util.*;
import java.sql.*;
import mystuff.*;

/**
 *
 * @author Sam Griffiths
 */
public class revaDAO {
    
    //initializes variables
    private static revaDAO instance;
    private String revid;
    private static Cache cache;
    
    /** Creates a new instance of revaDAO */
    public revaDAO() {
    cache = Cache.getInstance();
    }
    
    //returns an instance of revaDAO
    public static synchronized revaDAO getInstance() {
        if (instance == null) {
            instance = new revaDAO();
            
        }
        return instance;
    }
    
    //creaets a new revBO with an id and ya
    public revBO create(){
        revid = Guid.generate();
        revBO rev = new revBO(revid);
        cache.put(revid, rev);
        return rev;
    }
    
    //retrieves a connection and then hands that and the revid of the 
    //revenue object to be looked up to another read method
    public synchronized void read(String revid) throws DataException{
        revBO str = null;
        Connection conn = null;
        try {
            conn = ConnectionPool.getInstance().get();
            read(conn, revid);
            
            ///////////////////////////////////////////
            // call read with a connection (the other read method in this class)
            
            // release the connection
            conn.commit();
            ConnectionPool.getInstance().release(conn);
            
        }catch (ConnectionPoolException e){
            throw new DataException("Could not get a connection to the database.");
            
        }catch (SQLException e) {
            // rollback
            try {
                conn.rollback();
                ConnectionPool.getInstance().release(conn);
            }catch (SQLException e2) {
                throw new DataException("Big error: could not even release the connection", e2);
            }catch (ConnectionPoolException e3){
            }
            
            throw new DataException("Could not retrieve records", e);
        }
    }
    
    //exactly like readALL(), but only reads/returns one instance of a revBO
    public synchronized void read(Connection conn, String id){
       revaBO rev = (revaBO)cache.get(revid);
        try{
        Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM REV_TABLE WHERE REVID='"+id+"'");
            while(rs.next()){
                rev.setPrice(Double.valueOf(rs.getString("PRICE").trim()).doubleValue());
                rev.setAlreadyInDatabase(true);
            }
       }catch(Exception e){}
    }

    
    /////////////save////////////////////
    //checks to see if object is dirty, or and retrieves a connection, then calls another save
    public synchronized boolean save(revBO bo) throws DataException {
       System.out.println("Save entrance accessed");
        Connection conn = null;
        try {
            
            conn = ConnectionPool.getInstance().get();
            if(bo.isDirty())
                save(conn,bo);
            bo.setDirty(false);
            
            ///////////////////////////////////////////
            // call read with a connection (the other read method in this class)
            
            // release the connection
            conn.commit();
            ConnectionPool.getInstance().release(conn);
            
        }catch (ConnectionPoolException e){
            throw new DataException("Could not get a connection to the database.");
            
        }catch (SQLException e) {
            // rollback
            try {
                conn.rollback();
                ConnectionPool.getInstance().release(conn);
            }catch (SQLException e2) {
                throw new DataException("Big error: could not even release the connection", e2);
            }catch (ConnectionPoolException e3){
            }
            
            throw new DataException("Could not retrieve records", e);
        }
        
        return true;
        }
    
    //checks to see if already in database, result = insert or update of BO
    public synchronized void save(Connection conn, revBO bo){
        System.out.println("Second Save Accessed");
        try{
            Statement st = conn.createStatement();
            String revid = "";
           
            System.out.println("First Query Skipped");
            if(false == bo.isAlreadyInDatabase()){
                
                String queryString = "INSERT INTO REV_TABLE VALUES (?,?,?)";
                PreparedStatement pstmt = conn.prepareStatement(queryString);
               
                //String aString = Double.toString(aDouble);
                pstmt.setString(1,bo.getRevenueId());
                pstmt.setString(2,Double.toString(bo.getPrice()));
                pstmt.setString(3,bo.getType());
                pstmt.execute();
                pstmt.close();
                bo.setAlreadyInDatabase(true);
            } else{
            
                String queryString = "UPDATE REV_TABLE SET PRICE = ?, TYPE = ? WHERE REVID = ? ";
                PreparedStatement pstmt = conn.prepareStatement(queryString);
                pstmt.setString(1,Double.toString(bo.getPrice()));
                pstmt.setString(2,bo.getType());
                pstmt.setString(3,bo.getRevenueId());
                pstmt.execute();
                pstmt.close();
                
            }
        }catch(Exception e){}
    }
    
    /////////////delete////////////////////////
    /*
        We don't need a delete for revenue source, especially since it's just a little black box
     */
    
}
